#include "task.h"
#include <algorithm>
#include <cctype>
task::task(std::string a, std::string b, std::string c, 
           std::string d, std::string e) :
    name(a), start_time(b), notice_time(c), id(b), level(d), element(e) {}

bool compare(task a, task b) {
    if(a.notice_time < b.notice_time) return true;
    else if(a.notice_time == b.notice_time && a.level > b.level) return true;
    else return false;
}

void sort(std::vector<task>& tasks) {
    int n = tasks.size();
    for(int i = 1; i < n; ++i) {
        bool flag = true;
        for(int j = 0; j < n - i; ++j) {
            if(compare(tasks[j], tasks[j+1])) {
                flag = false;
                task tmp = tasks[j];
                tasks[j] = tasks[j+1];
                tasks[j+1] = tmp;
            }
        }
        if(flag) break;
    }
}

void showtasks(std::vector<task>& tasks, std::string model) {
    sort(tasks);
    int n = tasks.size();
    time_t now = std::time(nullptr);
    tm* local_time = std::localtime(&now);
    char buffer[9];
    std::strftime(buffer, sizeof(buffer), "%Y%m%d", local_time);
    
    if(model == "day") {
        for(int i = 0; i < n; ++i) {
            bool flag = true;
            for(int j = 0; j < 8; j++) {
                if(buffer[j] != tasks[i].start_time[j]) {
                    flag = false;
                    break;
                }
            }
            if(flag) std::cerr << tasks[i].name << " " << tasks[i].start_time << " " 
                               << tasks[i].notice_time << " " << tasks[i].id << " " 
                               << tasks[i].level << " " << tasks[i].element << std::endl;
        }
    }
    if(model == "month") {
        for(int i = 0; i < n; ++i) {
            bool flag = true;
            for(int j = 0; j < 6; j++) {
                if(buffer[j] != tasks[i].start_time[j]) {
                    flag = false;
                    break;
                }
            }
            if(flag) std::cerr << tasks[i].name << " " << tasks[i].start_time << " " 
                               << tasks[i].notice_time << " " << tasks[i].id << " " 
                               << tasks[i].level << " " << tasks[i].element << std::endl;
        }
    }
    if(model=="all"){
    for(int i=0;i<n;++i){
       std::cerr << tasks[i].name << " " << tasks[i].start_time << " "
                               << tasks[i].notice_time << " " << tasks[i].id << " "
                               << tasks[i].level << " " << tasks[i].element << std::endl;
    }
    }
}

void addtask(std::vector<task>& tasks, std::string a, std::string b, 
             std::string c, std::string d, std::string e) {
    task t(a, b, c, d, e);
    tasks.push_back(t);
    int n = tasks.size();
    for(int i = n - 1; i > 0; --i) {
        if(compare(tasks[i], tasks[i-1])) {
            tasks[i] = tasks[i-1];
            tasks[i-1] = t;
        }
    }
}

void deltask(std::vector<task>& tasks, std::string id) {
    for(auto it = tasks.begin(); it != tasks.end(); ++it) {
        if(it->id == id) {
            tasks.erase(it);
            break;
        }
    }
}
bool isValidTimeFormat(const std::string& time) {
    if (time.length() != 12) return false;
    return std::all_of(time.begin(), time.end(), [](char c) { return std::isdigit(c); });
}

bool isValidDateTime(const std::string& timeStr) {

        int year = std::stoi(timeStr.substr(0,4));
        int month = std::stoi(timeStr.substr(4,2));
        int day = std::stoi(timeStr.substr(6,2));
        int hour = std::stoi(timeStr.substr(8,2));
        int minute = std::stoi(timeStr.substr(10,2));

        if (month < 1 || month > 12) return false;
        if (day < 1 || day > 31) return false;
        if (hour > 23) return false;
        if (minute > 59) return false;

        // 简单的月份天数验证（不考虑闰年等复杂情况）
        const int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
        return day <= daysInMonth[month-1];

}
